﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using DP.Web.UI;
using CRM.BLL;
using CRM.Entity;
using DP.Common;

namespace CRM.Web.MessagingManage
{
    public partial class TelSearch : Common.PageBase
    {

        private UserLoginInfo _userLoginInfo;
        T_PHONEBOOK_BLL bll = new T_PHONEBOOK_BLL();
        T_PHONEBOOKGROUP_BLL groupBll = new T_PHONEBOOKGROUP_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            if (!IsPostBack)
            {

                ddlPHONEBOOKGROUPIDBinding();

                GetUrlQueryToControl();
                gvDataBinding();
                gvDataBinding();
            }
        }




        /// <summary>
        ///	电话薄分组编号 下拉列表数据绑定
        /// </summary>
        private void ddlPHONEBOOKGROUPIDBinding()
        {
            ddlPHONEBOOKGROUPID.DataSource = groupBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = {0} and Status = {1}", _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE, ((int)Status.启用).ToString()));
            ddlPHONEBOOKGROUPID.DataTextField = "GROUPNAME";
            ddlPHONEBOOKGROUPID.DataValueField = "PHONEBOOKGROUPID";
            ddlPHONEBOOKGROUPID.DataBind();
            ddlPHONEBOOKGROUPID.Items.Insert(0, new ListItem("", ""));
        }




        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["BOOKID"]))
            {
                sql.Append(" and BOOKID = '" + Request.QueryString["BOOKID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["NAME"]))
            {
                sql.Append(" and NAME like '%" + Request.QueryString["NAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILEONE"]))
            {
                sql.Append(" and (MOBILEONE = '" + Request.QueryString["MOBILEONE"] + "' or MOBILETWO = '" + Request.QueryString["MOBILEONE"] + "' or HOMEPHONE = '" + Request.QueryString["MOBILEONE"] + "' or OFFICEPHONE = '" + Request.QueryString["MOBILEONE"] + "' ) ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILETWO"]))
            {
                sql.Append(" and MOBILETWO like '%" + Request.QueryString["MOBILETWO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HOMEPHONE"]))
            {
                sql.Append(" and HOMEPHONE like '%" + Request.QueryString["HOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OFFICEPHONE"]))
            {
                sql.Append(" and OFFICEPHONE like '%" + Request.QueryString["OFFICEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["URL"]))
            {
                sql.Append(" and URL like '%" + Request.QueryString["URL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QQ"]))
            {
                sql.Append(" and QQ like '%" + Request.QueryString["QQ"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MSN"]))
            {
                sql.Append(" and MSN like '%" + Request.QueryString["MSN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PHONEBOOKGROUPID"]))
            {
                sql.Append(" and PHONEBOOKGROUPID = '" + Request.QueryString["PHONEBOOKGROUPID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            }

            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            return sql;

        }
        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_PHONEBOOK_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "BOOKID", "NAME", "MOBILEONE" };
            gvList.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        protected void btnSelected_Click(object sender, EventArgs e)
        {
            StringBuilder sbTel = new StringBuilder();
            var list = GetSelected(gvList);
            foreach (var tel in list)
            {
                if (sbTel.Length != 0)
                {
                    sbTel.Append(";");
                }
                sbTel.Append(tel);
            }
            PageHelper.ExecuteJS(String.Format("window.opener.SetTel('{0}'); closeSelf(); ", sbTel.ToString()));
        }

        private List<string> GetSelected(GridView gv)
        {
            List<string> list = new List<string>();
            if (gv == null) return list;
            CheckBox cb;
            foreach (GridViewRow gvr in gv.Rows)
            {
                cb = gvr.FindControl("cbItem") as CheckBox;
                if (cb == null) continue;
                if (!cb.Checked) continue;
                list.Add(gv.DataKeys[gvr.RowIndex].Values["MOBILEONE"].ToString());
            }
            return list;
        }


        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "STATUS":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            temp = ((Status)Enum.Parse(typeof(Status), temp)).ToString();
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    case "PHONEBOOKGROUPID":
                                        {
                                            temp = groupBll.GetCacheGroupNameByKey(this.Page, e.Row.Cells[i].Text.Trim());
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
    }
}
